# significance tests
estimates <- testEstimates(analysis, extra.pars = T)
estimates
confint(estimates)
##################################################
# wald and likelihood ratio (omnibus) tests
##################################################
# wald test that all slopes = 0
nullanalysis <- with(implist, lmer(psolvepst ~ 1 + (1 | school)))
testModels(analysis, nullanalysis, method = "D1")
# analysis and pooling
implist <- as.mitml.list(split(imps, imps$imputation))
# example x: analyze two-level random intercept multiple imputations
library(fdir)
library(lme4)
library(mitml)
##################################################
# read imputed data
##################################################
# set working directory
set()
# read imputed data from working directory
imps <- read.table("./imps/imps.dat")
names(imps) <- c("imputation","school","student","condition","teachexp","eslpct","ethnic","male","frlunch",
"lowach","stanmath","efficacypre","efficacypst","psolvepre","psolvepst")
# center covariates at their grand means
imps$psolvepre.cgm <- imps$psolvepre - mean(imps$psolvepre)
imps$male.cgm <- imps$male - mean(imps$male)
imps$frlunch.cgm <- imps$frlunch - mean(imps$frlunch)
imps$teachexp.cgm <- imps$teachexp - mean(imps$teachexp)
##################################################
# analysis and pooling
##################################################
# analysis and pooling
implist <- as.mitml.list(split(imps, imps$imputation))
fit <- with(implist, lmer(psolvepst ~ psolvepre.cgm + male.cgm + frlunch.cgm + teachexp.cgm + condition + (1 | school), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates)
# Two-Level Regression With Random Intercepts
library(fdir)
library(lme4)
library(mitml)
##################################################
# read imputed data
##################################################
# set working directory
set()
# read imputed data from working directory
imps <- read.table("./imps/imps.dat")
names(imps) <- c("imputation","school","student","condition","teachexp","eslpct","ethnic","male","frlunch",
"lowach","stanmath","efficacypre","efficacypst","psolvepre","psolvepst")
# center covariates at their grand means
imps$psolvepre.cgm <- imps$psolvepre - mean(imps$psolvepre)
imps$male.cgm <- imps$male - mean(imps$male)
imps$frlunch.cgm <- imps$frlunch - mean(imps$frlunch)
imps$teachexp.cgm <- imps$teachexp - mean(imps$teachexp)
##################################################
# analysis and pooling
##################################################
# analysis and pooling
implist <- as.mitml.list(split(imps, imps$imputation))
fit <- with(implist, lmer(psolvepst ~ psolvepre.cgm + male.cgm + frlunch.cgm + teachexp.cgm + condition + (1 | school), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates) # confidence intervals
##################################################
# wald and likelihood ratio (omnibus) tests
##################################################
# wald test that all slopes = 0
nullanalysis <- with(implist, lmer(psolvepst ~ 1 + (1 | school)))
testModels(analysis, nullanalysis, method = "D1")
# Two-Level Regression With Random Intercepts
library(fdir)
library(lme4)
library(mitml)
##################################################
# read imputed data
##################################################
# set working directory
set()
# read imputed data from working directory
imps <- read.table("./imps/imps.dat")
names(imps) <- c("imputation","school","student","condition","teachexp","eslpct","ethnic","male","frlunch",
"lowach","stanmath","efficacypre","efficacypst","psolvepre","psolvepst")
# center covariates at their grand means
imps$psolvepre.cgm <- imps$psolvepre - mean(imps$psolvepre)
imps$male.cgm <- imps$male - mean(imps$male)
imps$frlunch.cgm <- imps$frlunch - mean(imps$frlunch)
imps$teachexp.cgm <- imps$teachexp - mean(imps$teachexp)
##################################################
# analysis and pooling
##################################################
# analysis and pooling
implist <- as.mitml.list(split(imps, imps$imputation))
fit <- with(implist, lmer(psolvepst ~ psolvepre.cgm + male.cgm + frlunch.cgm + teachexp.cgm + condition + (1 | school), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates) # confidence intervals
##################################################
# wald and likelihood ratio (omnibus) tests
##################################################
# wald test that all slopes = 0
nullanalysis <- with(implist, lmer(psolvepst ~ 1 + (1 | school)))
testModels(fit, nullanalysis, method = "D1")
# Two-Level Regression With Random Intercepts
library(fdir)
library(lme4)
library(mitml)
##################################################
# read imputed data
##################################################
# set working directory
set()
# read fcs imputed data from working directory
imps <- read.table("./imps/fcs.dat")
set()
# read fcs imputed data from working directory
imps <- read.table("./imps/fcsimps.dat")
names(imps) <- c("imputation","school","student","condition","teachexp","eslpct","hispanic","male","frlunch",
"lowach","stanmath","efficacypre","efficacypst","psolvepre","psolvepst")
# within-cluster (group mean) center sleep quality and add cluster-specific group means to the data
imps <- rockchalk::gmc(imps, c("psolvepre","hispanic","frlunch"), by = c("school"), FUN = mean, suffix = c(".meanj", ".cwc"), fulldataframe = TRUE)
imps$psolvepre.meanj.cgm <- imps$psolvepre.meanj - mean(imps$psolvepre.meanj)
imps$hispanic.meanj.cgm <- imps$hispanic.meanj - mean(imps$hispanic.meanj)
imps$frlunch.meanj.cgm <- imps$frlunch.meanj - mean(imps$frlunch.meanj)
implist <- as.mitml.list(split(imps, imps$imputation))
fit <- with(implist, lmer(psolvepst ~ psolvepre.cwc + hispanic.cwc + frlunch.cwc + psolvepre.meanj.cgm + hispanic.meanj.cgm + frlunch.meanj.cgm + condition + (1 | school), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates) # confidence intervals
# Two-Level Regression With Random Intercepts
library(fdir)
library(lme4)
library(mitml)
##################################################
# read imputed data
##################################################
# set working directory
set()
# read imputed data from working directory
imps <- read.table("./imps/imps.dat")
names(imps) <- c("imputation","school","student","condition","teachexp","eslpct","hispanic","male","frlunch",
"lowach","stanmath","efficacypre","efficacypst","psolvepre","psolvepst")
# within-cluster (group mean) center level-1 predictors and add cluster-specific group means to the data
imps <- rockchalk::gmc(imps, c("psolvepre","hispanic","frlunch"), by = c("school"), FUN = mean, suffix = c(".meanj", ".cwc"), fulldataframe = TRUE)
# center covariates at their grand means
imps$psolvepre.meanj.cgm <- imps$psolvepre.meanj - mean(imps$psolvepre.meanj)
imps$hispanic.meanj.cgm <- imps$hispanic.meanj - mean(imps$hispanic.meanj)
imps$frlunch.meanj.cgm <- imps$frlunch.meanj - mean(imps$frlunch.meanj)
##################################################
# analysis and pooling
##################################################
# analysis and pooling
implist <- as.mitml.list(split(imps, imps$imputation))
fit <- with(implist, lmer(psolvepst ~ psolvepre.cwc + hispanic.cwc + frlunch.cwc + psolvepre.meanj.cgm + hispanic.meanj.cgm + frlunch.meanj.cgm + condition + (1 | school), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates) # confidence intervals
##################################################
# wald and likelihood ratio (omnibus) tests
##################################################
# wald test that all slopes = 0
nullanalysis <- with(implist, lmer(psolvepst ~ 1 + (1 | school)))
testModels(fit, nullanalysis, method = "D1")
# Two-Level Regression With Random Intercepts
library(fdir)
library(lme4)
library(mitml)
##################################################
# read imputed data
##################################################
# set working directory
set()
# read imputed data from working directory
imps <- read.table("./imps/imps.dat")
names(imps) <- c("imputation","school","student","condition","teachexp","eslpct","hispanic","male","frlunch",
"lowach","stanmath","efficacypre","efficacypst","psolvepre","psolvepst")
# within-cluster (group mean) center level-1 predictors and add cluster-specific group means to the data
imps <- rockchalk::gmc(imps, c("psolvepre","hispanic","frlunch"), by = c("school"), FUN = mean, suffix = c(".meanj", ".cwc"), fulldataframe = TRUE)
# center covariates at their grand means
imps$psolvepre.meanj.cgm <- imps$psolvepre.meanj - mean(imps$psolvepre.meanj)
imps$hispanic.meanj.cgm <- imps$hispanic.meanj - mean(imps$hispanic.meanj)
imps$frlunch.meanj.cgm <- imps$frlunch.meanj - mean(imps$frlunch.meanj)
##################################################
# analysis and pooling
##################################################
# analysis and pooling
implist <- as.mitml.list(split(imps, imps$imputation))
fit <- with(implist, lmer(psolvepst ~ psolvepre.cwc + hispanic.cwc + frlunch.cwc + psolvepre.meanj.cgm + hispanic.meanj.cgm + frlunch.meanj.cgm + condition + (1 | school), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates) # confidence intervals
##################################################
# wald and likelihood ratio (omnibus) tests
##################################################
# wald test that all slopes = 0
nullanalysis <- with(implist, lmer(psolvepst ~ 1 + (1 | school)))
testModels(fit, nullanalysis, method = "D1")
# Two-Level Moderated Regression With Random Slopes
library(fdir)
library(mitml)
library(lme4)
##################################################
# read imputed data
##################################################
# set working directory
set()
# read imputed data from working directory
imps <- read.table("./imps/imps.dat")
names(imps) <- c("imputation","school","student","wave","condition","teachexp","eslpct","hispanic",
"male","frlunch","lowach","stanmath","month", "month7", "probsolve", "efficacy")
# center covariates at their grand means
imps$frlunch.cgm <- imps$frlunch - mean(imps$frlunch)
# compute product term
imps$monthbyc <- imps$month * imps$condition
imps$monthbyh <- imps$month * imps$hispanic
##################################################
# analysis and pooling
##################################################
# analysis and pooling
implist <- as.mitml.list(split(imps, imps$imputation))
fit <- with(implist, lmer(probsolve ~ month + frlunch.cgm + hispanic + condition + monthbyc + monthbyh + (1 + month | student), REML = T))
# significance tests
estimates <- testEstimates(fit, extra.pars = T)
estimates
confint(estimates) # confidence intervals
# test conditional effect (simple slope) at condition = 0
# slp_cond0 <- "month + monthbyc*0"
testConstraints(fit, constraints = "month + monthbyc*0")
# test conditional effect (simple slope) at condition = 1
# slp_cond1 <- "month + monthbyc*1"
testConstraints(fit, constraints = "month + monthbyc*1")
# test conditional effect (simple slope) at hispanic = 0
# slp_hisp0 <- "month + monthbyh*0"
testConstraints(fit, constraints = "month + monthbyh*0")
# test conditional effect (simple slope) at hispanic = 1
# slp_hisp1 <- "month + monthbyh*1"
testConstraints(fit, constraints = "month + monthbyh*1")
# Bayes Moderated Regression
library(fdir)
library(mitml)
##################################################
# read imputed data
##################################################
# set working directory
set()
# read imputed data from working directory
imps <- read.table("./imps/imps.dat")
names(imps) <- c("imputation", "id","male","hispanic","riskgrp","atrisk","behsymp1","lrnprob1",
"read1","read2","read3","read9","read9grp","stanread7",
"math1","math2","math3","math9","math9grp","stanmath7")
# center lower-order variables and compute the product
imps$read1.cgm <- imps$read1 - mean(imps$read1)
imps$lrnprob1.cgm <- imps$lrnprob1 - mean(imps$lrnprob1)
imps$atrisk.cgm <- imps$atrisk - mean(imps$atrisk)
imps$product <- imps$read1.cgm * imps$lrnprob1.cgm
##################################################
# analysis and pooling
##################################################
# analysis and pooling
implist <- as.mitml.list(split(imps, imps$imputation))
fit <- with(implist, lm(read9 ~ read1.cgm + lrnprob1.cgm + product + atrisk.cgm))
# significance tests with barnard & rubin degrees of freedom
estimates <- testEstimates(fit, extra.pars = T, df.com = 133)
estimates
confint(estimates) # confidence intervals
# pooled r-square
rsquare <- mean(unlist(lapply(implist, (function(x) summary(lm(read9 ~ read1.cgm + lrnprob1.cgm + product + atrisk.cgm, data = x))$r.squared))))
rsquare
##################################################
# test conditional effects (simple slopes)
##################################################
# pooled standard deviation of the moderator
lrnprob1.sd <- mean(unlist(lapply(implist, (function(x) sd(x$lrnprob1.cgm)))))
lrnprob1.sd
# test conditional effect (simple slope) at +1 SD above the mean
slp_high <- "read1.cgm + product*1*10.77"
testConstraints(fit, constraints = slp_high, df.com = 133)
# test conditional effect (simple slope) at the mean
slp_mean <- "read1.cgm + product*0*10.77"
testConstraints(fit, constraints = slp_mean, df.com = 133)
# test conditional effect (simple slope) at -1 SD below the mean
slp_low <- "read1.cgm + product*-1*10.77"
testConstraints(fit, constraints = slp_low, df.com = 133)
mymodel <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000)
# MCMC Moderated Regression
library(fdir)
library(rblimp)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
behaviorachievement <- read.table('behaviorachievement.dat', na.strings = 999)
names(behaviorachievement) <- c('id','male','hispanic','riskgrp','atrisk','behsymp1','lrnprob1',
'read1','read2','read3','read9','read9grp','stanread7',
'math1','math2','math3','math9','math9grp','stanmath7')
mymodel <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000)
mymodelp1 <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000,
options = 'prior1')
mymodelp3 <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000,
options = 'prior3')
output(mymodel)
output(mymodelp1)
output(mymodelp3)
output(mymodel)
output(mymodelp1)
output(mymodelp1)
mymodelp3 <- rblimp(
data = behaviorachievement,
ordinal = 'atrisk',
center = 'read1 lrnprob1 atrisk',
model = '
focal.model:
read9 ~ read1 lrnprob1 read1*lrnprob1 atrisk ;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 atrisk',
simple = 'read1 | lrnprob1',
seed = 90291,
burn = 5000,
iter = 10000,
options = 'prior3')
output(mymodelp3)
mymodel <- rblimp(
data = mathachievement,
ordinal = 'condition frlunch atrisk efficacy',
fixed = 'condition mathpre',
model = '
focal.model:
mathpost ~ condition@beta1 frlunch@beta2 efficacy@beta3 mathpre@beta4;
auxiliary.models:
stanread atrisk ~ mathpost condition frlunch efficacy mathpre',
waldtest = 'beta1:beta4 = 0',
seed = 90291,
burn = 5000,
iter = 10000)
# MCMC Regression With Binary and Ordinal Predictors
library(fdir)
library(rblimp)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
mathachievement <- read.table('mathachievement.dat', na.strings = 999)
names(mathachievement) <- c('id', 'condition', 'male', 'frlunch', 'atrisk', 'stanread', 'efficacy', 'anxiety', 'mathpre', 'mathpost')
##################################################
# fit model with mcmc (ex8.1.imp)
##################################################
mymodel <- rblimp(
data = mathachievement,
ordinal = 'condition frlunch atrisk efficacy',
fixed = 'condition mathpre',
model = '
focal.model:
mathpost ~ condition@beta1 frlunch@beta2 efficacy@beta3 mathpre@beta4;
auxiliary.models:
stanread atrisk ~ mathpost condition frlunch efficacy mathpre',
waldtest = 'beta1:beta4 = 0',
seed = 90291,
burn = 5000,
iter = 10000)
mymodel@output
mymodel1 <- rblimp(
data = mathachievement,
ordinal = 'condition frlunch atrisk efficacy',
fixed = 'condition mathpre',
model = '
focal.model:
mathpost ~ condition@beta1 frlunch@beta2 efficacy@beta3 mathpre@beta4;
auxiliary.models:
stanread atrisk ~ mathpost condition frlunch efficacy mathpre',
waldtest = 'beta1:beta4 = 0',
seed = 90291,
burn = 5000,
iter = 10000,
options = 'prior1')
mymodel1@output
mymodel3 <- rblimp(
data = mathachievement,
ordinal = 'condition frlunch atrisk efficacy',
fixed = 'condition mathpre',
model = '
focal.model:
mathpost ~ condition@beta1 frlunch@beta2 efficacy@beta3 mathpre@beta4;
auxiliary.models:
stanread atrisk ~ mathpost condition frlunch efficacy mathpre',
waldtest = 'beta1:beta4 = 0',
seed = 90291,
burn = 5000,
iter = 10000,
options = 'prior3')
mymodel3@output
mymodel@output
mymodel1@output
mymodel3@output
# MCMC Linear Regression
library(fdir)
library(rblimp)
library(mitml)
##################################################
# read data
##################################################
# set working directory
set()
# read imputed data from working directory
behaviorachievement <- read.table('behaviorachievement.dat', na.strings = 999)
names(behaviorachievement) <- c('id','male','hispanic','riskgrp','atrisk','behsymp1','lrnprob1',
'read1','read2','read3','read9','read9grp','stanread7',
'math1','math2','math3','math9','math9grp','stanmath7')
mymodel1 <- rblimp(
data = behaviorachievement,
model = '
focal.model:
read9 ~ read1@beta1 lrnprob1@beta2 behsymp1@beta3;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 behsymp1',
waldtest = 'beta1:beta3 = 0',
seed = 90291,
burn = 1000,
iter = 10000,
options = 'prior1')
mymodel1@output
mymodel2 <- rblimp(
data = behaviorachievement,
model = '
focal.model:
read9 ~ read1@beta1 lrnprob1@beta2 behsymp1@beta3;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 behsymp1',
waldtest = 'beta1:beta3 = 0',
seed = 90291,
burn = 1000,
iter = 10000,
options = 'prior2')
mymodel2@output
mymodel3 <- rblimp(
data = behaviorachievement,
model = '
focal.model:
read9 ~ read1@beta1 lrnprob1@beta2 behsymp1@beta3;
auxiliary.models:
stanread7 read2  ~ read9 read1 lrnprob1 behsymp1',
waldtest = 'beta1:beta3 = 0',
seed = 90291,
burn = 1000,
iter = 10000,
options = 'prior3')
mymodel3@output
mymodel1@output
mymodel1@output
mymodel2@output
mymodel3@output
